package com.deltapunkt.proxy;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Message implements Event {
	private final byte[] content;
	private final SocketChannel s;

	public Message(SocketChannel s, ByteBuffer buffer) {
		this.s = s;
		int size = buffer.position();
		content = new byte[size];
		System.arraycopy(buffer.array(), 0, content, 0, size);
		Logger.it.logHex("read>" + s.toString(), content);
	}

	public Message(SocketChannel s, byte[] buffer) {
		this.s = s;
		content = buffer;
		Logger.it.logHex("write>" + s.toString(), content);
	}

	public Message(SocketChannel s, String str) {
		this.s = s;
		content = str.getBytes();
		// Charset.forName("").newDecoder();
	}

	public byte[] getContent() {
		return content;
	}

	public String getContentAsString() {
		try {
			return new String(content, "ASCII7");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public ByteBuffer getContentAsBuffer() {
		return ByteBuffer.wrap(content);
	}

	public SocketChannel getChannel() {
		return s;
	}
}
